﻿Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Word
Public Class LIMITES
    Dim ObjWord As Microsoft.Office.Interop.Word.Application

    Private Sub Substitui_Var(ByVal Header As String, ByVal Data As String)
        With ObjWord.Selection.Find
            .ClearFormatting()
            .Text = Header
            .Execute(Forward:=True)
        End With
        Clipboard.Clear()
        Clipboard.SetText(Data)
        ObjWord.Selection.Paste()
        Clipboard.Clear()
    End Sub
    Private Sub LIMITES_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.LIMITESTableAdapter1.Fill(Me.LIMITESDataSet1.LIMITES)
    End Sub

    Private Sub PRIMEIRO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PRIMEIRO.Click
        LIMITESBindingSource1.MoveFirst()
    End Sub

    Private Sub VOLTAR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VOLTAR.Click
        LIMITESBindingSource1.MovePrevious()
    End Sub

    Private Sub NOVO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NOVO.Click
        LIMITESBindingSource1.AddNew()
        CEDENTETextBox.Focus()
    End Sub

    Private Sub SALVAR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SALVAR.Click
        Try
            Me.Validate()
            LIMITESBindingSource1.EndEdit()
            TableAdapterManager1.UpdateAll(Me.LIMITESDataSet1)
        Catch EX As Exception
            MsgBox("ERRO NA ATUALIZAÇÃO DOS DADOS " & EX.ToString)
        End Try
    End Sub

    Private Sub DELETAR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DELETAR.Click
        Try
            Me.Validate()
            LIMITESBindingSource1.RemoveCurrent()
            LIMITESTableAdapter1.Update(Me.LIMITESDataSet1.LIMITES)
        Catch EX As Exception
            MsgBox("ERRO NA REMOÇÃO DOS DADOS " & EX.ToString)
        End Try
    End Sub

    Private Sub FRENTE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FRENTE.Click
        LIMITESBindingSource1.MoveNext()
    End Sub

    Private Sub ULTIMO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ULTIMO.Click
        LIMITESBindingSource1.MoveLast()
    End Sub

    Private Sub TODOS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TODOS.Click
        Me.LIMITESTableAdapter1.Fill(Me.LIMITESDataSet1.LIMITES)
    End Sub

    Private Sub CEDENTE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CEDENTE.Click
        LIMITESTableAdapter1.FillByCEDENTE(LIMITESDataSet1.LIMITES, CEDENTETextBox.Text)
    End Sub

    Private Sub CARTA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CARTA.Click
        Dim salvarComo As SaveFileDialog = New SaveFileDialog()
        Dim caminho As DialogResult
        Dim Arquivo As String
        Dim a As System.String
        If Date.Now.Month = 1 Then
            a = "JANEIRO"
        ElseIf Date.Now.Month = 2 Then
            a = "FEVEREIRO"
        ElseIf Date.Now.Month = 3 Then
            a = "MARÇO"
        ElseIf Date.Now.Month = 4 Then
            a = "ABRIL"
        ElseIf Date.Now.Month = 5 Then
            a = "MAIO"
        ElseIf Date.Now.Month = 6 Then
            a = "JUNHO"
        ElseIf Date.Now.Month = 7 Then
            a = "JULHO"
        ElseIf Date.Now.Month = 8 Then
            a = "AGOSTO"
        ElseIf Date.Now.Month = 9 Then
            a = "SETEMBRO"
        ElseIf Date.Now.Month = 10 Then
            a = "OUTUBRO"
        ElseIf Date.Now.Month = 11 Then
            a = "NOVEMBRO"
        Else
            a = "DEZEMBRO"
        End If

        If CEDENTETextBox.Text = "" Or LIMITECEDENTETextBox.Text = "" Or LIMITEPFTextBox.Text = "" Or LIMITEPFTextBox.Text = "" Or TAXATextBox.Text = "" Or CONTATOTextBox.Text = "" Then
            MsgBox("Favor preencher todos so campos")
        Else
            salvarComo.Title = "Arquivos Word |*.docx |"
            caminho = salvarComo.ShowDialog
            Arquivo = salvarComo.FileName

            On Error GoTo trata_erro

            ObjWord = New Word.Application
            ObjWord.Documents.Open("\\SERVIDOR1\Publico Administrativo\Público Administrativo\CARTA CEDENTES NOVOS.doc")

            Call Substitui_Var("@DIA", Date.Now.Day)
            Call Substitui_Var("@MES", a)
            Call Substitui_Var("@ANO", Date.Now.Year)
            Call Substitui_Var("@CEDENTE", CEDENTETextBox.Text)
            Call Substitui_Var("@CONTATO", CONTATOTextBox.Text)
            Call Substitui_Var("@TAXA", TAXATextBox.Text)
            Call Substitui_Var("@LIMITECEDENTE", LIMITECEDENTETextBox.Text)
            Call Substitui_Var("@LIMITEPJ", LIMITEPJTextBox.Text)
            Call Substitui_Var("@LIMITEPF", LIMITEPFTextBox.Text)
            ObjWord.ActiveDocument.SaveAs(Arquivo)
            ObjWord.Quit()
            MsgBox("Contrato gerado com sucesso! em : " & Arquivo, vbInformation, " Contrato Gerado ")
            ObjWord = Nothing
            Exit Sub
trata_erro:
            ObjWord.Quit()
            MsgBox("Ocorreu um erro durante o processamento " & " - Erro numero : " & Err.Number)
        End If
    End Sub
    Private Sub LIMITECEDENTETextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles LIMITECEDENTETextBox.LostFocus
        LIMITECEDENTETextBox.Text = Format(LIMITECEDENTETextBox.Text, "CURRENCY")
    End Sub

    Private Sub LIMITEPFTextBox_BorderStyleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles LIMITEPFTextBox.BorderStyleChanged

    End Sub

    Private Sub LIMITEPFTextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles LIMITEPFTextBox.LostFocus
        LIMITEPFTextBox.Text = Format(LIMITEPFTextBox.Text, "CURRENCY")
    End Sub

    Private Sub LIMITEPJTextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles LIMITEPJTextBox.LostFocus
        LIMITEPJTextBox.Text = Format(LIMITEPJTextBox.Text, "CURRENCY")
    End Sub
End Class